﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using Annuaire;
using annuairedotnet;

public partial class AffContactPerso : System.Web.UI.Page
{
    Contact contactFromRequest = null;
    int idFromRequest = 0;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Roles.IsUserInRole("user"))
        {
            panel.Enabled = false;
            CalendarExtender1.Enabled = false;
        }

        idFromRequest = Convert.ToInt32(Request["id"]);

        if (!Page.IsPostBack){
           
            string urlReferent = (string)Session["urlReferent"];
            string urlReferentMail = (string)Session["urlReferentMail"];

            if ((urlReferent != null) && ((urlReferent.Equals("AffContactPro.aspx")) || (urlReferentMail.Equals("EnvoiMail.aspx"))))
            {
                tbIdentifiant.Text = ((Contact)Session["contact"]).Id.ToString();
                dlCivilite.Text = ((Contact)Session["contact"]).Civilite;
                tbNom.Text = ((Contact)Session["contact"]).Nom;
                tbPrenom.Text = ((Contact)Session["contact"]).Prenom;
                tbNaissance.Text = ((Contact)Session["contact"]).DateNaiss;
                tbAdresse.Text = ((Contact)Session["contact"]).DonneesPerso.AdressePerso;
                tbCp.Text = ((Contact)Session["contact"]).DonneesPerso.CpPerso;
                tbVille.Text = ((Contact)Session["contact"]).DonneesPerso.VillePerso;
                tbTelephone.Text = ((Contact)Session["contact"]).DonneesPerso.TelephonePerso;
                tbEmail.Text = ((Contact)Session["contact"]).DonneesPerso.EmailPerso;
                iPhoto.ImageUrl = ((Contact)Session["contact"]).DonneesPerso.Photo;
                if (((Contact)Session["contact"]).Interets.Jardinage == true)
                {
                    cbJardinage.Checked = true;
                }
                if (((Contact)Session["contact"]).Interets.Musique == true)
                {
                    cbMusique.Checked = true;
                }
                if (((Contact)Session["contact"]).Interets.Bricolage == true)
                {
                    cbBricolage.Checked = true;
                }
            }else{

                 if (idFromRequest != 0)
                 {
                     contactFromRequest = ContactService.getById(idFromRequest);
                     tbIdentifiant.Text = contactFromRequest.Id.ToString();
                     dlCivilite.Text = contactFromRequest.Civilite;
                     tbNom.Text = contactFromRequest.Nom;
                     tbPrenom.Text = contactFromRequest.Prenom;
                     tbNaissance.Text = contactFromRequest.DateNaiss;
                     tbAdresse.Text = contactFromRequest.DonneesPerso.AdressePerso;
                     tbCp.Text = contactFromRequest.DonneesPerso.CpPerso;
                     tbVille.Text = contactFromRequest.DonneesPerso.VillePerso;
                     tbTelephone.Text = contactFromRequest.DonneesPerso.TelephonePerso;
                     tbEmail.Text = contactFromRequest.DonneesPerso.EmailPerso;
                     iPhoto.ImageUrl = contactFromRequest.DonneesPerso.Photo;
                     if (contactFromRequest.Interets.Jardinage == true)
                     {
                         cbJardinage.Checked = true;
                     }
                     if (contactFromRequest.Interets.Musique == true)
                     {
                         cbMusique.Checked = true;
                     }
                     if (contactFromRequest.Interets.Bricolage == true)
                     {
                         cbBricolage.Checked = true;
                     }
                 }    
            }
        }
    
    }

    protected void btSuivant_Click(object sender, EventArgs e)
    {
        Contact contact = new Contact();
        contact.Id = idFromRequest;
        contact.Civilite = dlCivilite.Text;
        contact.Nom = tbNom.Text;
        contact.Prenom = tbPrenom.Text;
        contact.DateNaiss = tbNaissance.Text;
        
        DonneesPerso donneesPerso = new DonneesPerso();
        donneesPerso.AdressePerso = tbAdresse.Text;
        donneesPerso.CpPerso = tbCp.Text;
        donneesPerso.VillePerso = tbVille.Text;
        donneesPerso.TelephonePerso = tbTelephone.Text;
        donneesPerso.EmailPerso = tbEmail.Text;
        donneesPerso.Photo = (string)Session["urlPhoto"];

        contact.DonneesPerso = donneesPerso;

        if (cbBricolage.Checked || cbJardinage.Checked || cbMusique.Checked)
        {
            Interet interets = new Interet();

            if (cbBricolage.Checked)
            {
                interets.Bricolage = true;
            }

            if (cbJardinage.Checked)
            {
                interets.Jardinage = true;
            }

            if (cbMusique.Checked)
            {
                interets.Musique = true;
            }

            contact.Interets = interets;
        }

        Session.Add("contact", contact);
        Session.Add("pageResult", this);
        Server.Transfer("AffContactPro.aspx");
    }

    protected void btAnnuler_Click(object sender, EventArgs e)
    {
        if (idFromRequest != 0)
        {
            Session.Add("urlReferent", "AffContactPerso.aspx");
       }
        else
        {
            Session.Remove("urlReferent");
        }
        Response.Redirect("RechContact.aspx");
    }

    protected void btEmail_Click(object sender, ImageClickEventArgs e)
    {
        Session.Add("mail", tbEmail.Text);
        btEmail.Attributes.Add("onclick", "window.open('EnvoiMail.aspx',null,'left=300, top=100, height=625, width= 900, status=n o, resizable= no, scrollbars= no, toolbar= no,location= no, menubar= no');");
    }

    protected void btPhoto_Click(object sender, EventArgs e)
    {
        if (fuPhoto.HasFile) 
        {
            Session.Remove("urlPhoto");
            string dossierPhoto = "B:/Mes Projets/Annuaire2/tmp/";
            fuPhoto.SaveAs(dossierPhoto + fuPhoto.FileName);
            iPhoto.ImageUrl = "tmp/" + fuPhoto.FileName;
            Session.Add("urlPhoto", iPhoto.ImageUrl);
        }
    }
}
